from django.http import JsonResponse
from .models import *
from django.core.exceptions import ValidationError,ObjectDoesNotExist
def get_event_list(request):
    eid=request.GET.get('eid','')
    name=request.GET.get('name','')
    if eid=='' and name=='':
        return JsonResponse({'status':10022,'message':'parameter error'})
    if eid!='':
        event={}
        try:
            result=Event.objects.get(id=eid)
        except ObjectDoesNotExist:
            return JsonResponse({'status':10022,'message':'query result is Empty!!!'})
        else:
            event['name']=result.name
            event['limit']=result.limit
            event['startus']=result.startus
            event['address']=result.address
            event['start_time']=result.start_time
            event['create_time']=result.create_time
            return JsonResponse({'status':10023,'message':'success','data':event})
    if name!='':
        datas=[]
        result=Event.objects.filter(name__contains=name)
        if result:
            for r in result:
                event={}
                event['name']=r.name
                event['limit']=r.limit
                event['startus']=r.startus
                event['address']=r.address
                event['start_time']=r.start_time
                event['create_time']=r.create_time
                datas.append(event)
            return JsonResponse({'status':200,'message':'successfuly!!','data':datas})
        else:
            return JsonResponse({'status':10022,'message':'query result is Empty!!!'})

def add_event(request):
    eid=request.GET.get('eid','')
    name=request.GET.get('name','')
    limit=request.GET.get('limit','')
    startus=request.GET.get('startus','')
    address=request.GET.get('address','')
    start_time=request.GET.get('start_time','')
    create_time=request.GET.get('create_time','')
    if eid=='' or name=='' or limit=='' or startus=='' or address=='' or start_time=='' or create_time=='':
        return JsonResponse({'status':10021,'message':'parameters error!!!'})
    result=Event.objects.filter(id=eid)
    if result:
        return JsonResponse({'status':10022,'message':'Event id is already exits!!!'})
    result=Event.objects.filter(name=name)
    if result:
        return JsonResponse({'status': 10023, 'message': 'Event name is already exits!!!'})
    if startus=='':
        startus=1
    try:
        Event.objects.create(id=eid,name=name,limit=limit,startus=int(startus),address=address,start_time=start_time,create_time=create_time)
    except ValidationError as e:
        return JsonResponse({'status':10024,'message':e})
    return JsonResponse({'status':200,'message':'add event successfuly!!'})

def add_guest(request):
    eid=request.POST.get('eid')
    realname=request.POST.get('realname')
    phone=request.POST.get('phone')
    email=request.POST.get('email')
    if eid=="" or realname=="" or phone=="" or email=="":
        return JsonResponse({'status':10021,'msg':'参数错误!!!!'})
    result=Event.objects.filter(id=eid)
    if not result:
        return JsonResponse({'status':10022,"msg":"event id not "})
    result=Event.objects.get(id=eid).status
    if not result:
        return JsonResponse({'status':10023,'msg':'发布会不可用'})
    event_limit=Event.objects.get(id=eid).limit
    guest_limit=Guest.objects.filter(event_id=eid)
    if len(guest_limit)>=event_limit:
        return JsonResponse({'status':10024,'msg':'达到人数上限'})
    try:
        Guest.objects.create(realname=realname,phone=phone,email=email)
    except BaseException:
        return JsonResponse({'status':10025,'msg':'手机号重复！！'})




